package com.mask.common.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.mask.common.core.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "createBy", this::getCurrentUsername, String.class);
        this.strictInsertFill(metaObject, "updateBy", this::getCurrentUsername, String.class);
        this.strictInsertFill(metaObject, "delFlag", () -> "0", String.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, "updateBy", this::getCurrentUsername, String.class);
    }

    /**
     * 获取当前用户名
     */
    private String getCurrentUsername() {
        try {
            return SecurityUtils.getCurrentUsername();
        } catch (Exception e) {
            return "system";
        }
    }
}

// # MyBatis Plus配置
// mybatis-plus:
//   mapper-locations: classpath*:/mapper/**/*.xml
//   typeAliasesPackage: com.mask.**.entity
//   global-config:
//     banner: false
//     db-config:
//       id-type: auto
//       logic-delete-field: delFlag
//       logic-delete-value: 1
//       logic-not-delete-value: 0
//   configuration:
//     map-underscore-to-camel-case: true
//     cache-enabled: false
//     call-setters-on-nulls: true
//     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

// # 动态数据源配置
// spring:
//   datasource:
//     dynamic:
//       primary: master
//       datasource:
//         master:
//           url: jdbc:mysql://localhost:3306/mask?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
//           username: root
//           password: 123456
//           driver-class-name: com.mysql.cj.jdbc.Driver
//         slave:
//           url: jdbc:mysql://localhost:3307/mask?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
//           username: root
//           password: 123456
//           driver-class-name: com.mysql.cj.jdbc.Driver
// @Data
// @TableName("sys_user")
// public class User {
//     @TableId(type = IdType.AUTO)
//     private Long id;
//
//     private String username;
//
//     @TableField(fill = FieldFill.INSERT)
//     private LocalDateTime createTime;
//
//     @TableField(fill = FieldFill.INSERT_UPDATE)
//     private LocalDateTime updateTime;
//
//     @TableField(fill = FieldFill.INSERT)
//     private String createBy;
//
//     @TableField(fill = FieldFill.INSERT_UPDATE)
//     private String updateBy;
//
//     @TableLogic
//     private String delFlag;
// }


// @Mapper
// public interface UserMapper extends BaseMapper<User> {
//     @DataScope(deptAlias = "d", userAlias = "u")
//     List<User> selectUserList(Page<User> page, UserDTO user);
// }
// @Service
// public class UserService extends ServiceImpl<UserMapper, User> {
    
//     @DS("slave")  // 切换数据源
//     public List<User> selectUserList(UserDTO user) {
//         Page<User> page = new Page<>(user.getPageNum(), user.getPageSize());
//         return baseMapper.selectUserList(page, user);
//     }
// }